﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_OrderPolicy")]
    public class P_OrderPolicy : IEntity<P_OrderPolicy>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int OrderId { get; set; }
        public int PolicyId { get; set; }
        public int AddType { get; set; }
        public int RefId { get; set; }

        public override P_OrderPolicy Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.Id == Id);
        }

        public override P_OrderPolicy Find(System.Linq.Expressions.Expression<Func<P_OrderPolicy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.OrderPolicy.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.OrderId = item.OrderId;
                    this.PolicyId = item.PolicyId;
                    this.AddType = item.AddType;
                    this.RefId = item.RefId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_OrderPolicy, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.OrderPolicy.Any(where);
            }
        }
    }
}
